﻿(function () {
    'use strict';

    angular
        .module('app.patientdemo')
        .factory('PatientDemoData', PatientDemoData);

    PatientDemoData.$inject = ['$rootScope','$http'];

    /* @ngInject */
    function PatientDemoData($rootScope, $http) {
        var service = {

            GetPatientForCategory: GetPatientForCategory
            , GetPatientDemo: GetPatientDemo
            , SavePatientData: SavePatientData
            , GetReferralProcessDetail: GetReferralProcessDetail
            , SaveReferralProcessStatus: SaveReferralProcessStatus
            , GetReferralLog: GetReferralLog
            , DeleteReferrral: DeleteReferrral
            , GetAdvancedSearchResults: GetAdvancedSearchResults
            , GetAdvancedSearchResultsByPage: GetAdvancedSearchResultsByPage
            , SetAdvancedSearchResult: SetAdvancedSearchResult
            , GetReferringPhysicianList: GetReferringPhysicianList
            //showToasts: true,

            //error: error,
            //info: info,
            //success: success,
            //warning: warning,

            //// straight to console; bypass toastr
            //log: $log.log
        };

        return service;
        /////////////////////
        var PatientDemo;
        var SearchResult;

        function GetPatientForCategory(type, category) {
            
            if(type == 'Search'){
                var data = "{'LoginID':'" + $rootScope.CurrentLoginID + "','SearchText':'" + category + "'}";
                return $http.post(apiLocation + 'SearchList', data);
            }
            if (type == 'Referral') {
                var data = "{'LoginID':'" + $rootScope.CurrentLoginID + "','ReferralID':'" + category + "'}";
                return $http.post(apiLocation + 'ReferralByID', data);
            }
            else{
            var data = "{'LoginID':'" + $rootScope.CurrentLoginID + "','ReferralType':'" + type + "','PracticeName':'" + category + "'}";
                return $http.post(apiLocation + 'ReferralList', data);
            }
            
        }

        function GetPatientDemo(ReferralID) {
            if (!ReferralID) {
                ReferralID = 0;
            }
            return $http.get(apiLocation + 'PatientDemographics/' + ReferralID);
        }

        function GetReferralLog(ReferralID) {
            if (!ReferralID) {
                ReferralID = 0;
            }
            return $http.get(apiLocation + 'ReferralLog/' + ReferralID);
        }

        function SavePatientData(PatientID, FirstName, LastName, DOB, AddrLine1, AddrLine2, State, ZipCode, PhoneNumber, PhoneType, Email, PInsurance, PPlanNo, SInsurance, SPlanNo, Notes, RequestedPractice, ReferringPhysician, PreferredPhysician,CurrentLoginID,RefReason,IsUrgentRequest,PrefPhone,EstablishedPatient) {
            var data = "{'PatientID':'" + PatientID + "','PatientFirstName':'" + FirstName + "','PatientLastName':'" + LastName + "','DOB':'" + DOB + "','AddrLine1':'" + AddrLine1 + "','AddrLine2':'" + AddrLine2 + "','State':'" + State + "','ZipCode':'" + ZipCode + "','PhoneNumber':'" + PhoneNumber + "','PhoneType':'" + PhoneType + "','Email':'" + Email + "','Insurance':'" + PInsurance + "','Insurance1':'" + SInsurance + "','PlanNumber':'" + PPlanNo + "','PlanNumber1':'" + SPlanNo + "','Notes':'" + Notes + "','RequestedPractice':'" + RequestedPractice + "','PhysicianID':'" + ReferringPhysician + "','PreferredPhysician':'" + PreferredPhysician + "','LoginID':'" + CurrentLoginID + "','RefReason':'" + RefReason + "','IsUrgentRequest':'" + IsUrgentRequest + "','PrefPhone':'" + PrefPhone + "','EstablishedPatient':'" + EstablishedPatient + "'}";
            return $http.post(apiLocation + 'PatientDemographics', data);
        }

        function GetReferralProcessDetail(ReferralID) {
            if (!ReferralID) {
                ReferralID = 0;
            }
            return $http.get(apiLocation + 'ProcessRequest/' + ReferralID);
        }

        function SaveReferralProcessStatus(ReferralID, AcceptanceStatus, AcceptanceNotes, ScheduleStatus, ScheduleNotes, VisitStatus, VisitNotes, ReferredPhysician, RequestedPractice , ApptDate, ApptTime, NoteStatus, NoteSentDate) {
            var data = "{'ReferralID':'" + ReferralID + "','AcceptanceStatus':'" + AcceptanceStatus + "','AcceptanceNotes':'" + AcceptanceNotes + "','VisitStatus':'" + VisitStatus + "','VisitNotes':'" + VisitNotes + "','ScheduleStatus':'" + ScheduleStatus + "','ScheduleNotes':'" + ScheduleNotes + "','Physician':'" + ReferredPhysician + "','Practice':'" + RequestedPractice + "','LoginID':'" + $rootScope.CurrentLoginID + "','AppointmentDate':'" + ApptDate + "','AppointmentTime':'" + ApptTime + "','NoteStatus':'" + NoteStatus + "','NoteSentDate':'" + NoteSentDate + "'}";
            return $http.post(apiLocation + 'ProcessRequest', data);
        }

        function GetReferringPhysicianList()
        {
            return $http.get(apiLocation + '/' + ReferralID);
        }

        function DeleteReferrral(ReferralID)
        {
            if (!ReferralID) {
                ReferralID = 0;
            }

            var data = "{'ReferralID':'" + ReferralID + "','LoginID':'" + $rootScope.CurrentLoginID + "'}";
            return $http.post(apiLocation + 'DeleteReferral', data);
        }

        function GetAdvancedSearchResults(FromDate,ToDate,ReferralType,ReferralStatus,FromPractice,ToPractice,FromPhysician,ToPhysician)
        {
            var data = "{'LoginID':'" + $rootScope.CurrentLoginID + "','FromDate':'" + FromDate + "','ToDate':'" + ToDate + "','ReferralType':'" + ReferralType + "','ReferralStatus':'" + ReferralStatus + "','FromPractice':'" + FromPractice + "','ToPractice':'" + ToPractice + "','FromPhysician':'" + FromPhysician + "','ToPhysician':'" + ToPhysician + "'}";
            return $http.post(apiLocation + 'AdvancedSearch', data);
        }

        function GetAdvancedSearchResultsByPage(pagenumber, pagelimit)
        {
            var PageList = SearchResult.slice(pagenumber * pagelimit, (pagenumber * pagelimit) + pagelimit);
            return PageList;
        }

        function SetAdvancedSearchResult(DefaultList) {
            SearchResult = DefaultList;
            return SearchResult.length;
        }
    }
}());
